<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use think\Db;
class Products extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';
    /**
     * @var int|mixed
     */
    private $category_id;
    /**
     * @var int|mixed
     */
    private $series_id;
    /**
     * @var int|mixed
     */
    private $texture_id;
    /**
     * 产品详情ID
     * @var mixed|null
     */
    private $id;

    public function _initialize()
    {
        parent::_initialize();

        $param = $this->request->param();
        $lanmu = isset($param['lanmu'])?$param['lanmu']:1;
        $category_id = isset($param['category_id'])?$param['category_id']:0;
        $series_id = isset($param['series_id'])?$param['series_id']:0;
        $texture_id = isset($param['texture_id'])?$param['texture_id']:0;
        $id = isset($param['id'])?$param['id']:null;

        $this->category_id = $category_id;
        $this->series_id = $series_id;
        $this->texture_id = $texture_id;

        $this->view->assign('id', $id);

    }

    public function index()
    {
        $list = Db::name('productslist')->where('shswitch',1);
        if (!empty($this->category_id)) {
            $list = $list->where('productstype',$this->category_id);
        }
        if (!empty($this->series_id)) {
            $list = $list->where('series_id',$this->series_id);
        }
        if (!empty($this->texture_id)) {
            $list = $list->where('texture_id',$this->texture_id);
        }
        $list = $this->getContentByLang($list->order('weigh','desc')
            ->order('id','desc')
            ->select());
        foreach ($list as $key => $item) {
            $list[$key]['price'] = Db::name('specification')->where('product_id',$item['id'])->min('spec_price');
        }
        $this->view->assign('list', $list);

        $section['jianjie'] = $this->getContentByLang(Db::name('danye')->where('id',20)->find());
        $section['gushi'] = $this->getContentByLang(Db::name('danye')->where('id',21)->find());
        $series = $this->getContentByLang(Db::name('series')->where('id',$this->series_id)->find());
        $this->view->assign('series', $series);
        $this->view->assign('section', $section);
        return $this->view->fetch();
    }

    public function show()
    {
        $param = $this->request->param();
        $id = isset($param['id'])?$param['id']:null;
        $preview = isset($param['preview'])?$param['preview']:null;

        $row = Db::name('productslist')->where('id',$id);
        if (empty($preview)) {
            $row = $row->where('shswitch',1);
        }
        $row = $this->getContentByLang($row->find());
        if (empty($row)) {
            exit('Not Found');
        }

        $row['series'] = $this->getContentByLang(Db::name('series')->where('id',$row['series_id'])->find());
        $row['category'] = $this->getContentByLang(Db::name('productstype')->where('id',$row['productstype'])->find());

        $spec1_items = [];
        $spec2_items = [];
        $spec_prices = [];
        $specs = $this->getContentByLang(Db::name('specification')->where('product_id',$id)->select());
        foreach ($specs as $spec) {
            $spec1_items[] = $spec['spec_name'];
            $spec2_items[] = $spec['spec_name2'];
            $spec_prices[$spec['spec_name']][$spec['spec_name2']] = [
                'price' => (float) $spec['spec_price'],
                'spec_image' => $spec['spec_image'],
                'url' => $spec['url'],
            ];
        }

        $row['images'] = explode(",", $row['images']);

        // 相关产品
        $xgProducts = $this->getContentByLang(Db::name('productslist')->where('productstype',$row['productstype'])->where('id', '<>', $row['id'])->limit(10)->select());

        $this->view->assign('row', $row);
        $this->view->assign('xgProducts', $xgProducts);
        $this->view->assign('spec_prices', $spec_prices);
        $this->view->assign('spec1_items', array_unique($spec1_items));
        $this->view->assign('spec2_items', array_unique($spec2_items));
        return $this->view->fetch('show');

    }

    public function productstypeshow(){
        return $this->view->fetch('productstypeshow');
    }

}
